package monitTree;

import com.opensymphony.xwork2.ActionSupport;
import db_tool.DBConnection;
import org.apache.struts2.ServletActionContext;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;

public class QueryData extends ActionSupport {
	private String flag;
	private Integer pageNumber;
	private String monitID;
	@Override
	public String execute() throws IOException{
		System.out.println("           sdd "+monitID);
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		PrintWriter writer = ServletActionContext.getResponse().getWriter();
		String result = "";
		DBConnection db_connection = DBConnection.getInstance();
		
		Statement statement = null;
		Statement statement2 = null;
		ResultSet resultSet = null;
		ResultSet resultSet2 = null;
		String sql = null;
		String sql2 = null;
	    if(flag.equals("CTH")){
	      sql = "select SampleID from Catches where ID_WaterLayer='" + monitID + "'";
		  sql2 = "select * from Catches where SampleID='" ;

	    }else if(flag.equals("FSS")){
	      sql = "select SampleID from Fishes where ID_Catches='" + monitID + "'";
	      sql2 = "select * from Fishes where SampleID='";
	    }else if(flag.equals("FSE")){
	    	sql = "select SampleID from FishEggs where ID_Catches='" + monitID + "'";
	    	sql2 = "select * from FishEggs where SampleID='";
	    }
	    else if(flag.equals("PZO")){
	    	sql = "select SampleID from DominantZooplanktonSpecies where ID_Zooplankton='" + monitID + "'";
	    	sql2 = "select * from DominantZooplanktonSpecies where SampleID='";
	    }
	    else if(flag.equals("PPH")){
	    	sql = "select SampleID from DominantPhytoplanktonSpecies where ID_Phytoplankton='" + monitID + "'";
	    	sql2 = "select * from DominantPhytoplanktonSpecies where SampleID='";
	    }
	    else if(flag.equals("PBE")){
	    	sql = "select SampleID from DominantBenthosSpecies where ID_Benthos='" + monitID + "'";
	    	sql2 = "select * from DominantBenthosSpecies where SampleID='";
	    }
		try {
			statement = db_connection.getStatement();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
		     resultSet = statement.executeQuery(sql);
	
		    int i = pageNumber*5;
			 System.out.println("-------  "+pageNumber+"                 "+i+"               "+monitID);
		    while(i>=1&&resultSet.next()){
		    	i--;
	        }
			System.out.println("vvvvvvvvvvvvvvvvvvvvv333");
			if(i >= 1){
				writer.write("没有数据了");
                return null;
			}
		    List<String> list = new LinkedList<String>();
		    
			while(resultSet.next()){
				list.add(resultSet.getString(1));
			}
			System.out.println(list);
			for(String target : list){
				String sql3 = sql2 + target +"'";
				 statement2 = db_connection.getStatement();
				
				 resultSet2 = statement2.executeQuery(sql3);
				 String string1 ="" ;
				 while(resultSet2.next()){
						
						for(int j=1;j<=resultSet2.getMetaData().getColumnCount();j++){
						//	System.out.println("  "+resultSet2.getString(j)+"  ");
						  string1	=	resultSet2.getString(j);
							result+=string1+",";
						}
						 result+="_";
					}
			}
			if(result.length() == 1) {
				writer.write("没有数据了");
                return null;
			}
			writer.write(result.substring(0, result.length()-2));
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println("hehrhehehehehehe");
			e.printStackTrace();
		}
		return null;
	}
	public String getFlag(){
		return flag;
	}	
	public void setFlag(String Flag){
		flag = Flag;
	}
	public Integer getPageNumber(){
		return pageNumber;
	}
	public void setPageNumber(Integer Page){
		pageNumber = Page;
	}
	public String getMonitID(){
		return monitID;
	}
	public void setMonitID(String monitID){
		this.monitID = monitID;
	}
	
}
